package com.chaidb;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;

import com.db.DbConnection;

public class ShowAllShareMarkers {
	@SuppressWarnings({ "finally", "unchecked" })
	public ArrayList doShowAllShareMarkers(String userid)
	{
		System.out.println("I am here in ShowAllShareMarkers");
		String sql = "select * from marker_share where uid='" + userid +"';";
		ArrayList markers = new ArrayList();
		HashMap marker;
//		MyMarker marker;
		Connection con = null;
		System.out.println(sql);
		try{
			con = DbConnection.getInstance().getConnection();
			Statement st = con.createStatement();
			ResultSet markeridrs = st.executeQuery(sql);
			while(markeridrs.next())
			{
				String markerid = markeridrs.getString("mid");
				String sql2 = "select * from marker where id='" +markerid+"';";
				Statement st2 = con.createStatement();
				ResultSet query = st2.executeQuery(sql2);
				System.out.println(sql2);
				if(query.next())
				{
					marker = new HashMap<String,Object>();
					marker.put("id",query.getString("id"));
					marker.put("state", query.getString("state"));
					marker.put("markername", query.getString("markername"));
					marker.put("userid", query.getString("userid"));
					marker.put("type", query.getString("type"));
					marker.put("description", query.getString("description"));
					marker.put("longitude", query.getString("longitude"));
					marker.put("latitude", query.getString("latitude"));
					marker.put("musicurl", query.getString("musicurl"));
					marker.put("createtime", query.getString("createtime"));
				
					String sql3 = "select * from user where id='" +query.getString("userid")+"';";
					Statement st3 = con.createStatement();
					ResultSet query3 = st3.executeQuery(sql3);
					if(query3.next())
						marker.put("creater",query3.getString("username"));
					markers.add(marker);
//					marker = new MyMarker();
//					marker.id = query.getString("id");
//					marker.state = query.getString("state");
//					marker.markername = query.getString("markername");
//					marker.userid = query.getString("userid");
//					marker.type = query.getString("type");
//					marker.description = query.getString("description");
//					marker.longitude = query.getString("longitude");
//					marker.latitude = query.getString("latitude");
//					marker.musicurl = query.getString("musicurl");
//					marker.createtime = query.getString("createtime");
//					marker.musicurl = query.getString("musicurl");
//					String sql3 = "select * from user where id='" +query.getString("userid")+"';";
//					Statement st3 = con.createStatement();
//					ResultSet query3 = st3.executeQuery(sql3);
//					if(query3.next())
//						marker.creater = query3.getString("username");
//					markers.add(marker);
				}
			}
		}catch(SQLException e)
		{
			e.printStackTrace();
		}finally
		{
			try
			{
				con.close();
			}catch(SQLException e)
			{
				e.printStackTrace();
			}
			return markers;
		}
	}
		

}
